﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.IO;
using System.Windows.Forms;

namespace myWindowsMediaPlayer.Model
{
    class databaseMusic
    {
        private List<musicModel> musics;

        public databaseMusic()
        {
            Regex reg = new Regex(";+");
            myConfigManager conf = new myConfigManager();
            string tmp = conf.MusicPath;

            parseXMLFile();
            if (tmp != null)
                foreach (string path in reg.Split(tmp))
                    try
                    {
                        reloadXMLMusic(path);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                    }
        }

        public void reloadXMLMusic(string path)
        {
            if (path != null && path.Length > 0)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileSystemInfo[] infos = dir.GetFileSystemInfos();
                XDocument doc = XDocument.Load(".\\conf\\music.xml");

                foreach (FileSystemInfo info in infos)
                    if (info is DirectoryInfo)
                        reloadXMLMusic(path + "\\" + info.Name);
                    else if (info is FileInfo)
                        if (Path.GetExtension(path + "\\" + info.Name).Equals(".mp3"))
                        {
                            musicModel m = new musicModel
                            {
                                Artist = new artist
                                {
                                    Name = ""
                                },
                                Track = new track
                                {
                                    Name = info.Name,
                                    Path = path + "\\" + info.Name
                                },
                                Album = new album
                                {
                                    Path = path,
                                    Name = ""
                                }

                            };
                            if (musics.Contains(m) == false)
                            {
                                musics.Add(m);
                                doc.Root.Add(new XElement("Music", new XAttribute("artistName", m.Artist.Name),
                                    new XAttribute("albumPath", m.Album.Path),
                                    new XAttribute("albumName", m.Album.Name),
                                    new XAttribute("trackPath", m.Track.Path),
                                    new XAttribute("trackName", m.Track.Name), ""));
                                doc.Save(".\\conf\\music.xml");
                            }
                            else
                                m = null;
                        }
            }
        }

        public List<musicModel> Musics
        {
            get { return this.musics; }
        }

        public void parseXMLFile()
        {
            try
            {
                XDocument doc = XDocument.Load(".\\conf\\music.xml");
                this.musics = (from music in doc.Root.Descendants("Music")
                               select new musicModel
                               {
                                   Track = new track
                                   {
                                       Path = music.Attribute("trackPath").Value,
                                       Name = music.Attribute("trackName").Value
                                       //Kind = music.Attribute("trackKind").Value
                                   },
                                   Album = new album
                                   {
                                       Path = music.Attribute("albumPath").Value,
                                       Name = music.Attribute("albumName").Value
                                   },
                                   Artist = new artist
                                   {
                                       Name = music.Attribute("artistName").Value
                                   }
                               }).ToList<musicModel>();
            }
            catch (Exception exc)
            {
                if (!Directory.Exists(".\\conf"))
                    Directory.CreateDirectory(".\\conf");
                if (!File.Exists(".\\conf\\music.xml"))
                    File.WriteAllText(".\\conf\\music.xml", "<musicFile>\n</musicFile>");
                System.Windows.Forms.MessageBox.Show(exc.ToString());
                parseXMLFile();
            }
        }
    }
}
